package com.tianjipms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianjipms.entity.Notice;
import com.tianjipms.service.NoticeService;
import com.tianjipms.utils.Result;
import com.tianjipms.vo.NoticeQueryVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/notice")
public class NoticeController {
    @Resource
    private NoticeService noticeService;

    // 新增或更新
    @PostMapping
    public Result addNotice(@RequestBody Notice notice) {
        noticeService.saveOrUpdate(notice);
        return Result.ok();
    }

    // 删除公告
    @DeleteMapping("/delete/{id}")
    public Result deleteNotice(@PathVariable int id) {
        if (noticeService.removeById(id)){
            return Result.ok().message("公告删除成功");
        }
        return Result.error().message("公告删除失败");
    }

    // 批量删除公告
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        noticeService.removeByIds(ids);
        return Result.ok();
    }

//    // 修改公告
//    @PutMapping("/update")
//    public Result updateNotice(@RequestBody Notice notice) {
//        if (noticeService.updateById(notice)){
//            return Result.ok().message("公告修改成功");
//        }
//        return Result.ok().message("公告修改失败");
//    }

    // 查询公告
    @GetMapping("/listAll")
    public Result listAll() {
        return Result.ok(noticeService.list());
    }


    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.ok(noticeService.getById(id));
    }

    @GetMapping("/list")
    public Result list(NoticeQueryVo noticeQueryVo){
        IPage<Notice> page = new Page<>(noticeQueryVo.getPageNum(),noticeQueryVo.getPageSize());
        noticeService.findNoticeListByPage(page, noticeQueryVo);
        return Result.ok(page);
    }
}
